/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#include "blockopt.h"

#include "em.h"
#include "fastonebigheader.h"
#include "qfunc.h"

#include <string.h>

float
blockopt (uint64_t                              t,
          float                                 eta,
          float                                 rho,
          uint64_t*                             last_t,
          unsigned int                          n_workers,
          float*                                dloga,
          float*                                dc,
          float*                                dd,
          unsigned int                          n_items,
          float*                                loga,
          float*                                gamma,
          unsigned int                          rank,
          float*                                logbeta,
          float*                                c,
          unsigned int                          n_labels,
          float*                                d,
          float*                                pz,
          const float*                          logpriorz,
          int                                   clamp,
          const Rating*                         ratings,
          unsigned int                          n_ratings,
          const ParameterizedNormalDistribution* priorloga,
          const Distribution*                   priorlogbeta,
          const Distribution*                   priorc,
          const Distribution*                   priord,
          bool                                  test_only)
{
  /* In the typical crowdsourcing setup a worker can only work on 
   * an item once, so don't try to guard against duplicate workers.
   */

  /* Step 1: apply loga prior sparsely
   * NB: this is just a (cheap!) approximation
   */

  /* NB: Limit rho->1 exists, but I'm lazy. */

  if (! test_only)
    {
      float powt = fastpow (t, 1.0f - rho);
      float sigma = priorloga->sq_stddev; 

      for (unsigned int i = 0; i < n_ratings; ++i)
        {
          uint64_t s = last_t[ratings[i].worker];
          float pows = fastpow (s, 1.0f - rho);
          float decay = 
            fastexp (eta * (pows - powt) / (n_items * (1.0f - rho) * sigma));

          float* logaptr = loga + ratings[i].worker * rank;

          for (unsigned int j = 0; j < rank; ++j)
            {
              float dlogatmp = (1.0 - decay) * (gamma[j] - logaptr[j]);
              logaptr[j] += dlogatmp;
              gamma[j] += dlogatmp / (1.0 + n_workers);
            }

          last_t[ratings[i].worker] = t;
        }
    }

  /* Step 2: maximize per-item objective wrt logbeta and pz */

  float q = em (loga, rank, logbeta, c, n_labels, d, pz, logpriorz, clamp,
                ratings, n_ratings, priorlogbeta, 100, 1e-3);

  if (! test_only)
    {
      /* Step 3: gradient update of loga and gamma, c, and d */

      for (unsigned int i = 0; i < n_ratings; ++i)
        {
          memset (dloga + ratings[i].worker * rank,
                  0,
                  rank * sizeof (dloga[0]));
        }

      memset (dc, 0, rank * n_labels * sizeof (dc[0]));
      memset (dd, 0, rank * n_labels * sizeof (dd[0]));

      dqfunc (dloga, dc, dd, 
              loga, rank, *logbeta, c, n_labels, d, pz, ratings, n_ratings);

      float etat = eta * fastpow (t, -rho);

      for (unsigned int i = 0; i < n_ratings; ++i)
        {
          float* logaptr = loga + ratings[i].worker * rank;
          float* dlogaptr = dloga + ratings[i].worker * rank;

          /* this is where duplicate workers would cause multiple updates */

          for (unsigned int j = 0; j < rank; ++j)
            {
              logaptr[j] += etat * dlogaptr[j];
              gamma[j] += (etat / (1.0 + n_workers)) * dlogaptr[j];
            }
        }

      /* priorgamma is assumed Gaussian with unit variance
       * and has already been incorporated into step 1
       */

      for (unsigned int j = 0; j < rank * n_labels; ++j)
        {
          float dpriorcj = 
            (1.0 / n_items) * distribution_dlogpdf (priorc, c[j]);

          float dpriordj = 
            (1.0 / n_items) * distribution_dlogpdf (priord, d[j]);

          c[j] += etat * (dc[j] + dpriorcj);
          d[j] += etat * (dd[j] + dpriordj);
        }
    }

  return q;
}

void
finalize (uint64_t                              t,
          float                                 eta,
          float                                 rho,
          uint64_t*                             last_t,
          unsigned int                          n_workers,
          unsigned int                          n_items,
          float*                                loga,
          float*                                gamma,
          unsigned int                          rank,
          const ParameterizedNormalDistribution* priorloga)
{
  /* Step 1: apply loga prior sparsely
   * NB: this is just a (cheap!) approximation
   */

  /* NB: Limit rho->1 exists, but I'm lazy. */

  float powt = fastpow (t, 1.0f - rho);
  float sigma = priorloga->sq_stddev; 

  for (unsigned int w = 0; w < n_workers; ++w)
    {
      uint64_t s = last_t[w];
      float pows = fastpow (s, 1.0f - rho);
      float decay = 
        fastexp (eta * (pows - powt) / (n_items * (1.0f - rho) * sigma));

      float* logaptr = loga + w * rank;

      for (unsigned int j = 0; j < rank; ++j)
        {
          float dlogatmp = (1.0 - decay) * (gamma[j] - logaptr[j]);
          logaptr[j] += dlogatmp;
          gamma[j] += dlogatmp / (1.0 + n_workers);
        }

      last_t[w] = t;
    }
}
